﻿using ICSharpCode.TextEditor;
using ICSharpCode.TextEditor.Document;
using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

namespace controls.TextEditor
{
    public partial class UserControl1 : UserControl
    {
        private TextEditorControl textEditor = null;
        public string filename;

        public UserControl1()
        {
            InitializeComponent();
            filename = @"D:\all\常用资料\time\1982-12-04 工作记录.txt";

            textEditor = new TextEditorControl();
            textEditor.Document.FoldingManager.FoldingStrategy = new FoldingTime();
        }

        public UserControl1(string filename)
        {
            InitializeComponent();
            this.filename = filename;

            textEditor = new TextEditorControl();
            textEditor.Document.FoldingManager.FoldingStrategy = new FoldingTime();
        }

        public UserControl1(string filename, string fording)
        {
            InitializeComponent();
            this.filename = filename;

            textEditor = new TextEditorControl();

            if (fording == "@@")
            {
                textEditor.Document.FoldingManager.FoldingStrategy = new FoldingCode();
            }
        }

        private void TextEditor_TextChanged(object sender, EventArgs e)
        {
            //更新，以便进行代码折叠
            textEditor.Document.FoldingManager.UpdateFoldings(null, null);
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="keyData"></param>
        /// <returns></returns>
        protected override bool ProcessCmdKey(ref System.Windows.Forms.Message msg, Keys keyData)
        {
            if (keyData == (Keys.Control | Keys.S))
            {
                textEditor.SaveFile(filename);
                //return base.ProcessCmdKey(ref msg, keyData);
            }
            else if (keyData == (Keys.Control | Keys.D))
            {
                return false;
            }
            else if (keyData == Keys.F5)
            {
                textEditor.LoadFile(filename);

                string path = Application.StartupPath + "\\HighLighting";
                FileSyntaxModeProvider fsmp;
                if (Directory.Exists(path))
                {
                    fsmp = new FileSyntaxModeProvider(path);
                    HighlightingManager.Manager.AddSyntaxModeFileProvider(fsmp);
                    textEditor.SetHighlighting("time");
                }
            }

            return base.ProcessCmdKey(ref msg, keyData);
        }

        private void UserControl1_Load(object sender, EventArgs e)
        {
            textEditor.Dock = DockStyle.Fill;
            this.Controls.Add(textEditor);
            textEditor.TextChanged += TextEditor_TextChanged;
            textEditor.KeyDown += textEditor_KeyDown;

            textEditor.Encoding = System.Text.Encoding.UTF8;
            textEditor.Font = new Font("宋体", 9);

            textEditor.ShowVRuler = false;

            textEditor.LoadFile(filename);

            string path = Application.StartupPath + "\\HighLighting";
            FileSyntaxModeProvider fsmp;
            if (Directory.Exists(path))
            {
                fsmp = new FileSyntaxModeProvider(path);
                HighlightingManager.Manager.AddSyntaxModeFileProvider(fsmp);
                //textEditor.SetHighlighting("time");
            }

            this.Disposed += UserControl1_Disposed;
        }

        private void UserControl1_Disposed(object sender, EventArgs e)
        {
            //throw new NotImplementedException();
            //this.textEditor = null;
            this.textEditor.Dispose();
        }

        private void textEditor_KeyDown(object sender, KeyEventArgs e)
        {
            //throw new NotImplementedException();

            //if (e.Modifiers == Keys.Control)
            //{
            //    if (e.KeyCode == Keys.S)
            //    {
            //        textEditor.SaveFile(@"D:\all\常用资料\time\1982-12-04 工作记录.txt");
            //        e.SuppressKeyPress = true;
            //    }
            //}
        }
    }
}